package jtk.project4.fleet.screen.tireInventory;

import java.awt.FlowLayout;
import java.util.ArrayList;
import jtk.project4.fleet.action.AddTireToInventoryAction;
import jtk.project4.fleet.action.DeleteTireAction;
import jtk.project4.fleet.action.EditTireInventoryAction;
import jtk.project4.fleet.action.TireCloseAction;
import jtk.project4.fleet.domain.TireInv;
//import jtk.project4.fleet.event.AddTireInventoryEvent;
import jtk.project4.fleet.event.TireInventoryEvent;
//import jtk.project4.fleet.event.AddTireInventoryEvent.AddTireInventoryEventType;
import jtk.project4.fleet.event.TireInventoryEvent.TireInventoryEventType;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.action.ActionGroup;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.SearchField;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.CheckButton;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.i18n.Message;
import nl.coderight.jazz.i18n.Message.Choice;
import nl.coderight.jazz.i18n.Message.Param;
import nl.coderight.jazz.view.StatusBar;

	public class TireInventoryView extends FormView{
		private TableField<TireInv> tireTableField;
		private TableField<TireInv> filterTireField;

		
		private Action addTireToInventoryAction;
		private Action edittireAction;
		private Action deletetireAction;
		private ActionGroup edittireActionGroup;
		private Action closeAction;

		
		private Message status;
		private StatusBar statusBar;
		private PushButton helpButton;
		private PushButton closeButton;
		private PushButton addtirePushButton;
		private PushButton edittirePushButton;
		private PushButton deletetirePushButton;
		private PushButton printtirePushButton;
		private PushButton filtertirePushButton;
		private PushButton searchtirePushButton;
		private CheckButton check;
		
		private SearchField filterField;
		private TireInventoryFilter tireFilter;


		
		public TireInventoryView (){
			setSize(700,600);
			center();
			tireFilter = new TireInventoryFilter();
		}
		
		public void handleEvent(TireInventoryEvent evt) {
			TireInv selectedTireInv = tireTableField.getValue();
			switch(evt.getType()){
			case CLOSE:
				close();
			case CHANGE:
				//bwt filter
			case SELECT :
				ArrayList<Choice> choices = new ArrayList<Choice>();
				choices.add(new Choice(0, "status.message.none"));
				choices.add(new Choice(1, "status.message.one"));
				choices.add(new Choice(2, "status.message.all"));
				
				status = new Message("status.message");
				status.addParam(new Param(tireTableField.getValueCount()));
				status.addParam(new Param(tireTableField.getListOfValues().size(), choices));
				status.addParam(new Param(tireTableField.getSelectionCount()));
				// display message on status bar
				statusBar = new StatusBar();
				statusBar.setMessage(status);
		

			break;
			case DELETE:
				Message message = new Message("dialog.delete.message");
				message.addParam(new Param(selectedTireInv.getNotes()));
				message.addParam(new Param(selectedTireInv.getTireId()));
				
				OptionDialog dialog = new OptionDialog(this);
				dialog.setTitle("dialog.delete.title");
				dialog.setOptionType(OptionType.YES_NO);
				dialog.setOptions("button.yes", "button.no", "button.cancel");
				dialog.setDefaultOption("button.no");
				dialog.setMessageType(MessageType.QUESTION);
				dialog.setMessage(message);
				dialog.show();			
				if (dialog.getReturnOptionType() == ReturnOptionType.YES) {
					propagateEvent(evt);
				}
				break;
			case FILTER:
				// filterField is changed, update filter
				tireFilter.setValue(filterField.getValue());
				break;
				
			default:
				// propagate event to controller
				propagateEvent(evt);
				break;
			

			}
		}
		
		
		@Override
		protected void init() {
			createAction();
			createFields();
			createLayout();		
		}	
		
		private void createAction() {
			closeAction = new TireCloseAction();
			edittireAction = new EditTireInventoryAction();	
			deletetireAction = new DeleteTireAction();
			// enable submit button when form is changed
			//setOnChangeAction(submittireAction);
			addTireToInventoryAction = new AddTireToInventoryAction();
			edittireActionGroup = new ActionGroup();
			edittireActionGroup.addAction(edittireAction);
			edittireActionGroup.addAction(deletetireAction);
				
		}
		
		private void createFields(){
			filterField = new SearchField(25);
			filterField.setInputPrompt("filter.prompt");
			filterField.setOnSearchEvent(new TireInventoryEvent(TireInventoryEventType.FILTER));
			
			tireTableField = new TableField<TireInv>("tireInvs","tireInv"); 
			tireTableField.setTitle("tire.header.table");
			tireTableField.setAutoResizeMode(AutoResizeMode.OFF);
			tireTableField.getSelectionValues();
			
			tireTableField.setOnSelectEvent(new TireInventoryEvent(TireInventoryEventType.SELECT));
			tireTableField.setOnChangeEvent(new TireInventoryEvent(TireInventoryEventType.CHANGE));
			tireTableField.setOnEditEvent(new TireInventoryEvent(TireInventoryEventType.EDIT));
			tireTableField.setOnSelectGroup(edittireActionGroup);	
			
			tireTableField.addColumn("label.serialNo","serialNo");
			tireTableField.addColumn("label.make","make");
			tireTableField.addColumn("label.model","model");
			tireTableField.addColumn("label.size","size");
			tireTableField.addColumn("label.tread","tread");
			tireTableField.addColumn("label.traction","traction");
			tireTableField.addColumn("label.load","load");
			tireTableField.addColumn("label.type","type");
			tireTableField.addColumn("label.status","status");
			tireTableField.addColumn("label.notes","notes");
			tireTableField.addFilter(tireFilter);
			
			addtirePushButton = new PushButton(addTireToInventoryAction);
			addtirePushButton.setToolTipText(addTireToInventoryAction.getLongDescription());
			edittirePushButton = new PushButton(edittireAction);
			deletetirePushButton = new PushButton(deletetireAction);
			//printtirePushButton= new PushButton("Print");
			//filtertirePushButton= new PushButton("Filter");
			//searchtirePushButton= new PushButton("Search");
			//check = new CheckButton("Show row filter");
			helpButton = new PushButton("Help");
			closeButton = new PushButton(closeAction);
		}

		
		private void createLayout() {
			// TODO Auto-generated method stub
			addField(createFieldsGroup(),CENTER);
		}

		private GroupControl createFieldsGroup() {
			// TODO Auto-generated method stub
			GroupControl fieldGroup = new GroupControl();
			fieldGroup.addField(tireTableField, NORTH);
			fieldGroup.addField(createToolBar(),CENTER);
			fieldGroup.addField(createButtons(), SOUTH);
			return fieldGroup;
		}
		private GroupControl createToolBar() {
			// TODO Auto-generated method stub
			GroupControl buttons = new GroupControl();
			buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
			buttons.addField(addtirePushButton);
			buttons.addField(edittirePushButton);
			buttons.addField(deletetirePushButton);
			//buttons.addField(printtirePushButton);
			//buttons.addField(filtertirePushButton);
			//buttons.addField(searchtirePushButton);
			//buttons.addField(check);
			buttons.addField(filterField);
			return buttons;
		}

		private GroupControl createButtons(){
			GroupControl buttons = new GroupControl();
			buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
			buttons.addField(helpButton);
			buttons.addField(closeButton);
			return buttons;
		}
		
		

	}


